package net.zjitc.controller;

import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.CartService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.impl.CartServiceImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/24.
 */
public class UserCart {
    CartService cartService = new CartServiceImpl();
    Product product = new Product();

    @URLMapping(url = "/findMyCart")
    public String findMyCart(HttpServletRequest request, HttpServletResponse response) {
        CategoryService service = new CategoryServiceImpl();
        List<Category> clist = service.findAllCate();
        request.setAttribute("clist", clist);
        //查询谁的购物车？？当前用户
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/WEB-INF/user/login.jsp";
        }

        List<CartItem> list = cartService.findByUid(user.getUid());
        request.setAttribute("list", list);

        double total = 0.0;
        if (list != null) {
            for (CartItem c : list) {
                total = total + c.getQuantity() * c.getProduct().getShop_price();
            }
        }
        request.setAttribute("total", total);
        return "/WEB-INF/user/cart.jsp";
    }

    @URLMapping(url = "/addCart")
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/WEB-INF/user/login.jsp";
        }
        Map<String, String[]> map = request.getParameterMap();
        try {
            CartItem Cartitem = new CartItem();
            Product product = new Product();

            BeanUtils.populate(Cartitem, map);
            BeanUtils.populate(product, map);
            Cartitem.setUser(user);
            Cartitem.setProduct(product);
            int count = cartService.add(Cartitem);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = request.getContextPath() + "/findMyCart";
        return "Redirect:" + url;
    }

    @URLMapping(url = "/deleteCart")
    public String deleteCart(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/WEB-INF/user/login.jsp";
        }
        CartService cartService = new CartServiceImpl();
        int pid = Integer.valueOf(request.getParameter("pid"));

        cartService.deleteCart(pid, user.getUid());
        return "/findMyCart";
    }

    @URLMapping(url = "/deleteAllCart")
    public String deleteAllCart(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "/WEB-INF/user/login.jsp";
        }
        CartService cartService = new CartServiceImpl();
        cartService.deleteAllCart(user.getUid());
        return "Redirect:" + request.getContextPath() + "/findMyCart";
    }
}